class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int sum = 0, res = 0;
        unordered_map<int, int> count;
        count[0] = 1;
        for (int i = 0; i < nums.size(); i++)
        {
            sum += nums[i];
            if(count.find(sum - k) != count.end())
                res += count[sum - k];
            if(count.find(sum) != count.end())
                count[sum]++;
            else
                count[sum] = 1;
        }
        return res;
    }
};
